#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int MAX_SIZE = 5;

// 学生类 - 专注于单个学生的属性和行为
class Student {
public:
    string num;
    string name;
    double math;
    double english;
    double cpp;
    double total;
    double ave;

    // 构造函数
    Student() {}

    // 设置学生信息
    void setValue() {
        cout << "请输入学生学号:" << endl;
        cin >> num;
        cout << "请输入学生姓名:" << endl;
        cin >> name;
        cout << "请输入学生数学成绩:" << endl;
        cin >> math;
        cout << "请输入学生英语成绩:" << endl;
        cin >> english;
        cout << "请输入学生C语言成绩:" << endl;
        cin >> cpp;
        calculate();
    }

    // 计算总分和平均分
    void calculate() {
        total = math + english + cpp;
        ave = total / 3.0;
    }

    // 显示学生信息
    void display() const {
        cout << num << "    " << name << "    " << math << "    " 
             << english << "    " << cpp << "    " << total << "    " << ave << endl;
    }

    // 修改学生成绩
    void modifyScore(const string& subject, double newScore) {
        if (subject == "math") {
            math = newScore;
        } else if (subject == "english") {
            english = newScore;
        } else if (subject == "cpp") {
            cpp = newScore;
        }
        calculate();
    }

    // 写入学生信息到文件流
    void writeToFile(ofstream& file) const {
        file << num << "    " << name << "    " << math << "    " 
             << english << "    " << cpp << "    " << total << "    " << ave << endl;
    }
};

// 学生管理类 - 管理学生集合
class StudentManager {
private:
    vector<Student> students;  // 存储所有学生

public:
    // 添加学生信息
    bool addStudent() {
        if (students.size() >= MAX_SIZE) {
            cout << "学生数量已达上限，无法继续输入。" << endl;
            return false;
        }

        Student newStudent;
        newStudent.setValue();
        students.push_back(newStudent);
        
        cout << "添加成功！当前学生列表：" << endl;
        displayByRank();
        
        if (students.size() < MAX_SIZE) {
            cout << "是否继续输入学生信息？" << endl;
            cout << "1. 继续输入" << endl;
            cout << "2. 结束输入" << endl;
            int choice;
            cin >> choice;
            if (choice == 1) {
                return addStudent(); // 递归调用继续添加
            }
        }
        
        return true;
    }
    
    // 显示表头
    void displayHeader() const {
        cout << "学号    姓名    数学    英语    C语言    总分    平均分" << endl;
        cout << "-----------------------------------------------------------------" << endl;
    }
    
    // 按平均分排名并显示学生信息
    void displayByRank() {
        if (students.empty()) {
            cout << "没有学生信息" << endl;
            return;
        }
        
        // 使用lambda表达式按平均分降序排序
        sort(students.begin(), students.end(), [](const Student& a, const Student& b) {
            return a.ave > b.ave;
        });
        
        displayHeader();
        for (const auto& student : students) {
            student.display();
        }
        
        // 排序后自动写入文件
        writeToFile();
    }
    
    // 将所有学生信息写入文件
    void writeToFile() const {
        ofstream file("../students.txt");
        if (file.is_open()) {
            file << "学号    姓名    数学    英语    C语言    总分    平均分" << endl;
            file << "-----------------------------------------------------------------" << endl;
            for (const auto& student : students) {
                student.writeToFile(file);
            }
            file.close();
        } else {
            cerr << "无法打开文件。" << endl;
        }
    }
    
    // 查找学生
    void searchStudent() {
        if (students.empty()) {
            cout << "没有学生信息" << endl;
            return;
        }
        
        cout << "请输入要查找的学生姓名" << endl;
        string targetName;
        cin >> targetName;
        
        bool found = false;
        for (const auto& student : students) {
            if (student.name == targetName) {
                if (!found) {
                    displayHeader();
                    found = true;
                }
                student.display();
            }
        }
        
        if (!found) {
            cout << "未找到该学生" << endl;
        }
    }
    
    // 修改学生信息
    void modifyStudent() {
        if (students.empty()) {
            cout << "没有学生信息" << endl;
            return;
        }
        
        cout << "请输入要修改的学生学号" << endl;
        string targetNum;
        cin >> targetNum;
        
        bool found = false;
        for (auto& student : students) {
            if (student.num == targetNum) {
                cout << "已找到该学生，请输入要修改的科目(math/english/cpp)" << endl;
                string subject;
                cin >> subject;
                
                if (subject == "math" || subject == "english" || subject == "cpp") {
                    cout << "修改该生的" << subject << "成绩，请输入修改后的成绩" << endl;
                    double newScore;
                    cin >> newScore;
                    
                    student.modifyScore(subject, newScore);
                    
                    cout << "该生修改后的成绩为" << endl;
                    displayHeader();
                    student.display();
                    found = true;
                } else {
                    cout << "无效的科目" << endl;
                }
                break;
            }
        }
        
        if (!found) {
            cout << "未找到该学生" << endl;
        }
        
        // 修改后重新排序显示
        displayByRank();
    }
    
    // 删除学生
    bool deleteStudent() {
        if (students.empty()) {
            cout << "没有学生信息可删除" << endl;
            return false;
        }
        
        cout << "请输入要删除的学生姓名" << endl;
        string targetName;
        cin >> targetName;
        
        auto it = remove_if(students.begin(), students.end(), [&targetName](const Student& s) {
            return s.name == targetName;
        });
        
        if (it != students.end()) {
            students.erase(it, students.end());
            cout << "已删除，其余学生成绩如下" << endl;
            displayByRank();
            return true;
        } else {
            cout << "未找到该学生" << endl;
            return false;
        }
    }
    
    // 判断是否为空
    bool isEmpty() const {
        return students.empty();
    }
};

// 显示菜单
void displayMenu() {
    cout << "-----------------------------------------------------------------" << endl;
    cout << "1. 输入学生信息" << endl;
    cout << "2. 按平均分排名" << endl;
    cout << "3. 查找学生信息" << endl;
    cout << "4. 修改学生信息" << endl;
    cout << "5. 删除学生信息" << endl;
    cout << "0. 退出" << endl;
    cout << "请输入要进行的操作的序号：";
}

int main() {
    StudentManager manager;
    int choice;
    
    do {
        displayMenu();
        cin >> choice;
        
        switch (choice) {
            case 1:
                manager.addStudent();
                break;
            case 2:
                manager.displayByRank();
                break;
            case 3:
                manager.searchStudent();
                break;
            case 4:
                manager.modifyStudent();
                break;
            case 5:
                manager.deleteStudent();
                break;
            case 0:
                cout << "退出程序" << endl;
                break;
            default:
                cout << "输入有误，请重新输入" << endl;
        }
    } while (choice != 0);
    
    return 0;
}